﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Threading;
using System.Globalization;
using Nivdal.AuxCode;


namespace Nivdal
{
    public partial class TeamPlayersDetails : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            OnlineUser ou = (OnlineUser)Session["User"];
            GetNivdalData.GetNivdalData GND = new GetNivdalData.GetNivdalData();
            bool IsNational = false;
            string TeamId = "";
            
            if (ou.TeamId == null)
                   Response.Redirect("~/Default.aspx");

            if (Request.QueryString["NationalTeam"] != null)
            {
                if (ou.NationalTeamCoach == "0")
                    Response.Redirect("~/Default.aspx");
                else
                {
                    IsNational = true;
                    TeamId = ou.NationalTeamCoach;
                }
            }
            else
            {
                TeamId = ou.TeamId;
            }

            string NumPlayers = GND.ReadNivdalStringData("@TeamId", "IsNational", "Nivdal_GetNumberOfTeamPlayers", TeamId, IsNational ? "1" : "0");

            if (!IsPostBack)
            {
                if (IsNational)
                {
                    TeamListOfPlayersSqlDataSource.SelectCommand = "Nivdal_NTGetListOfNationalTeamPlayers";
                }
                
                string TeamIdParamName = IsNational ? "NTTeamId" : "TeamId";
                this.TeamListOfPlayersSqlDataSource.SelectParameters.Add(TeamIdParamName, TeamId);
                this.TeamListOfPlayersSqlDataSource.SelectParameters.Add("Language", ou.Language);
            }

            string TeamName = "";
            if (IsNational)
            {
                TeamName = GND.ReadNivdalStringData("@NTTeamId", "Nivdal_NTGetNationalTeamNameById", TeamId);
                this.TeamHyperLink.NavigateUrl = "NationalTeam.aspx?Id=" + TeamId + "&National=yes";
            }
            else
            {
                TeamName = GND.ReadNivdalStringData("@TeamId", "Nivdal_GetTeamNameByTeamId", TeamId);
                this.TeamHyperLink.NavigateUrl = "TeamHomePage.aspx?TeamId=" + TeamId;
            }

            this.TeamHyperLink.Text = TeamName;

            this.NumberOfPlayersLabel.Text = NumPlayers + " " + Resources.Resource.TeamListOfPlayers_playersOf + " " + TeamName;
            this.Title = Resources.Resource.Default_PageTitle + ", " + TeamName + ": " + Resources.Resource.Global_Players;
            //this.Title = TeamName + " " + Resources.Resource.Global_Players + " - nivdal.com!";

            if (PlayersGridView.Rows.Count > 0)
                PlayersGridView.HeaderRow.TableSection = TableRowSection.TableHeader;
        }

        protected string InjuryIconName(object Injury)
        {
            GetNivdalData.GetNivdalData GND = new GetNivdalData.GetNivdalData();
            return GND.InjuryIconName(Injury);
        }

        protected override void InitializeCulture()
        {
            if (HttpContext.Current.User.Identity.IsAuthenticated)
            {

                OnlineUser ou = (OnlineUser)Session["User"];
                Thread.CurrentThread.CurrentUICulture = new CultureInfo(ou.UICulture);
                Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(ou.Culture);
            }
        }

        protected bool PrivateNoteVisible(object Note)
        {
            if (Note.ToString().Length == 0)
                return false;
            return true;
        }
    }

    
}